package ru.yermak.bookkeeping.model;

import org.hibernate.annotations.Index;
import org.hibernate.search.annotations.Field;
import org.hibernate.search.annotations.Store;
import org.hibernate.search.annotations.Indexed;
import org.hibernate.search.annotations.DocumentId;
import org.apache.lucene.document.Document;

import javax.persistence.*;
import java.util.List;
import java.util.Set;
import java.util.HashSet;

/**
 * User: harrier
 * Date: Aug 9, 2008
 */
@Entity
@Table(name = "accounts", uniqueConstraints = {@UniqueConstraint(columnNames = {"group_id", "name"})})
@Indexed(index = "accounts")
public class Account implements Selectable{
// ------------------------------ FIELDS ------------------------------

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @DocumentId
    private Integer id;

    @Basic
    @Column(name = "name", nullable = false, length = 64)
    private String name;

    @Basic
    @Column(name = "description", nullable = true, length = 1024)
    @Field(name = "AccountDesc", index = org.hibernate.search.annotations.Index.TOKENIZED, store = Store.NO)
    private String desc;

    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE})
    @JoinColumn(name = "group_id", nullable = false)
    @Index(columnNames = "group_id", name = "accounts_group_idx")
    private Group group;

    @OneToMany(mappedBy = "account", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<AccountValue> accountValues = new HashSet<AccountValue>();

// --------------------- GETTER / SETTER METHODS ---------------------

    public Set<AccountValue> getAccountValues() {
        return accountValues;
    }

    public void setAccountValues(Set<AccountValue> accountValues) {
        this.accountValues = accountValues;
    }

    public Group getGroup() {
        return group;
    }

    public void setGroup(Group group) {
        this.group = group;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void addAccountValue(AccountValue accountValue) {
        accountValues.add(accountValue);
    }
}
